﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Enterprise.Model.Manager;
using Enterprise.Service.Manager;
using Enterprise.Service.User;
namespace Enterprise.UI.Web.Modules.Manager
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OnBindList();
            OnBindOwnList();
        }

        private void OnBindList()
        {
            MsgService mService = new MsgService();
            List<MsgEntity> list= mService.MsgListByShow();
            OnBasebind(list, this.AspNetPager1, Repeater1);

        }
        private void  OnBindOwnList()
        {
            MsgService mService = new MsgService();
            List<MsgEntity> ownlist = mService.MsgListByOwn();
            OnBasebind(ownlist, this.AspNetPager2, this.Repeater2);
        }

        private void OnBasebind(List<MsgEntity> _list,Enterprise.Control.AspNetPager _pager, Repeater _repeater)
        {
            _pager.RecordCount = _list.Count;
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = _list;
            pds.PageSize = 10;
            pds.AllowPaging = true;
            pds.CurrentPageIndex = _pager.CurrentPageIndex - 1;
            _repeater.DataSource = pds;
            _repeater.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            OnBindList();
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            OnBindOwnList();
        }

        public string formatStr(int id)
        {
            StringBuilder sb = new StringBuilder();
            MsgReplyService rService = new MsgReplyService();
            List<MsgReplyEntity> list = rService.MsgReplyList(id);
            foreach (MsgReplyEntity reply in list)
            {
                sb.Append("<div style=\"width:95%; padding: 3px; background: none repeat scroll 0% 0% rgb(255, 255, 238); border: 1px solid rgb(153, 153, 153); overflow: hidden; margin-bottom: 3px; word-wrap: break-word;\"> <div style=\"line-height: 20px; height: 20px; overflow: hidden; position: relative; font-size: 12px; margin: 0pt 3px; clear: both;\"> <span style=\"color: rgb(30, 80, 162); padding: 5px 3px 0pt 0pt;\">回复(" + reply.ReplyTime + ")：</span><span style=\"color: rgb(102, 102, 102); position: absolute; right: 3px; top: 0pt;\">1</span> </div> <p style=\"margin: 8px 3px; line-height: 20px; clear: both; font-size: 14px;\">" + reply.ReplyContent + "</p></div>");
            }
            //sb.Append("<span onclick=\"Javascript:showPopWin('回复留言','ReplyMsg.aspx?id=" + id + "',600,300,AlertMessageBox,true,true);\" style=\"cursor:pointer;\">");
            //sb.Append("<img src=\"img/reply.png\" style=\"border:0px;\" alt=\"回复留言\" />");
            //sb.Append("</span>");
            sb.Append("<a href=\"ReplyMsg.aspx?Id="+id+"\"><img src=\"img/reply.png\" style=\"border:0px;\" alt=\"回复留言\" /></a>");
            return sb.ToString();
        }

        public string formatReply(int id)
        {
            StringBuilder sb = new StringBuilder();
            MsgReplyService rService = new MsgReplyService();
            List<MsgReplyEntity> list = rService.MsgReplyList(id);
            if (list.Count > 0)
            {
                foreach (MsgReplyEntity reply in list)
                {
                    sb.Append("<div style=\"width:95%; padding: 3px; background: none repeat scroll 0% 0% rgb(255, 255, 238); border: 1px solid rgb(153, 153, 153); overflow: hidden; margin-bottom: 3px; word-wrap: break-word;\"> <div style=\"line-height: 20px; height: 20px; overflow: hidden; position: relative; font-size: 12px; margin: 0pt 3px; clear: both;\"> <span style=\"color: rgb(30, 80, 162); padding: 5px 3px 0pt 0pt;\">回复(" + reply.ReplyTime + ")：</span></div> <p style=\"margin: 8px 3px; line-height: 20px; clear: both; font-size: 14px;\">" + reply.ReplyContent + "</p></div>");

                }
            }
            else
            {
                sb.Append("尚未回复");
            }
            return sb.ToString();
        }

        public string formatStrName(int id)
        {
            StringBuilder sb = new StringBuilder();
            MsgService mService = new MsgService();
            var q = mService.MsgDisp(id);
            if (q != null)
            {
                if (q.MsgIsShow == 0 || q.msgIsAnonymous == 1)
                {
                    sb.Append("匿名");
                }
                else
                {                    
                    sb.Append(UserService.GetUserName(q.MsgFUser));
                }
            }
            else
            {
                sb.Append("匿名");
            }
            return sb.ToString();
        }

    }
}